package com.yc.biz;

import com.yc.dao.CartInfoDao;
import com.yc.entity.CartInfo;
import com.yc.utile.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @program: freshgoodPro
 * @description:
 * @author: Joker
 * @create: 2023-09-16 18:38
 */
@Service
public class CartInfoBizImpl implements CartInfoBiz{
    @Autowired
    private CartInfoDao cartInfoDao;
    /**
     * 根据会员编号查看
     * @param mno
     * @return
     */
    @Override
    public List<Map<String, Object>> findByMno(Integer mno) {
        List<Map<String, Object>> list = null;
        try {
            list= cartInfoDao.findByMno(mno);
            for (int i = 0; i < list.size(); i++) {
                Map<String, Object> m = list.get(i);
                String pics = StringUtil.objToString(m.get("pics"));
                if (pics.contains(";")) {
                    pics = pics.split(";")[0];
                }
                m.put("pics", pics);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 添加购物车
     * @param cartInfo
     * @return
     */
    @Override
    public String addCart(CartInfo cartInfo) {
        //检查字符串是否为空
        if (StringUtil.checkNull(cartInfo.getGno().toString(), cartInfo.getMno().toString())) {
            return "0";
        }
        String cno = UUID.randomUUID().toString().replace("-", "");
        cartInfo.setCno(cno);
        int result = cartInfoDao.insert(cartInfo);

        if (result > 0) {
            return cno;
        }
        return "0";
    }
    /**
     * 根据购物车编号更新商品数量
     * @param cartInfo
     * @return
     */
    @Override
    public int update(CartInfo cartInfo) {
        int update = cartInfoDao.update(cartInfo);
        if(update > 0){
            return update;
        }else {
            return 0;
        }
    }
    /**
     * 根据购物车编号删除商品
     * @param cartInfo
     * @return
     */
    @Override
    public int del(CartInfo cartInfo) {
        int del = cartInfoDao.del(cartInfo);
        if(del >0){
            return del;
        }else {
            return 0;
        }
    }
}
